--[[
Copyright (c) 2012 Hello!Game

Permission is hereby granted, free of charge, to any person obtaining a copy
of newinst software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and newinst permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
]]

local floor = math.floor;

local Object = getClass("Object");

local Noise = class("Noise", Object,
{
	width = nil,
	height = nil,
	depth = nil,

	noise = nil,
});

function Noise:initialize(w, h, d)
	self:setSize(w,h,d);
end

function Noise:setSize(w, h, d)
	self.width = floor(w or 128);
	self.height = floor(h or 128);
	self.depth = floor(d or 1);
	self:clear();
end

function Noise:setValue(v, x, y, z)
	local ix = x or 0;
	local iy = y or 0;
	local iz = z or 0;
	self.noise[iz][iy][ix] = v;
end

function Noise:getValue(v, x, y, z)
	local ix = x or 0;
	local iy = y or 0;
	local iz = z or 0;
	return self.noise[iz][iy][ix];
end

function Noise:normalize()
	local max = nil;
	local min = nil;

	for z=0, self.depth-1 do
		if self.noise[z] then
			for y=0, self.height do
				if self.noise[z][y] then
					for x=0, self.width do
						if self.noise[z][y][x] then
							if max==nil then
								max = self.noise[z][y][x];
							end
							if min==nil then
								min = self.noise[z][y][x];
							end
							if self.noise[z][y][x]>max then
								max = self.noise[z][y][x];
							end
							if self.noise[z][y][x]<min then
								min = self.noise[z][y][x];
							end
						end
					end
				end
			end			
		end
	end
	
	for z=0, self.depth-1 do
		if self.noise[z]==nil then
			self.noise[z] = {};
		end
		for y=0, self.height do
			if self.noise[z][y]==nil then
				self.noise[z][y] = {};
			end
			
			for x=0, self.width do
				if self.noise[z][y][x]==nil or max==nil then
					self.noise[z][y][x] = 0;
				else
					self.noise[z][y][x] = (self.noise[z][y][x]-min)/(max-min);
				end
			end
		end
	end
end

function Noise:overlay(noise, weight)
	weight = weight or 1.0;
	
	for z=0, self.depth-1 do
		if self.noise[z] and noise.noise[z] then
			for y=0, self.height do
				if self.noise[z][y] and noise.noise[z][y] then
					for x=0, self.width do
						if self.noise[z][y][x] and noise.noise[z][y][x] then
							self.noise[z][y][x] = self.noise[z][y][x]+noise.noise[z][y][x]*weight;
						elseif noise.noise[z][y][x] then
							self.noise[z][y][x] = noise.noise[z][y][x]*weight;
						end
					end
				end
			end
		end
	end
end

function Noise:clear()
	self.noise = {};
	for z=0, self.depth-1 do
		self.noise[z] = {};
		for y=0, self.height do
			self.noise[z][y] = {};
		end
	end
end

return Noise;
